package mse.eai.esb;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.jboss.logging.Logger;
import org.jboss.soa.esb.actions.BeanConfiguredAction;
import org.jboss.soa.esb.message.Message;

import api.ejb.ISocializer;

@Stateless(name = "CallSocializerBean")
@Local
public class CallSocializerBean implements BeanConfiguredAction {

	// @EJB(mappedName = "java:/PhaseBook/ISocializer/local")
	ISocializer socializer;

	private static final Logger log = Logger
			.getLogger(CallSocializerBean.class);

	public Message process(Message message) {
		log.info("ESB Action start ------>");

		try {

			log.info("Calling the Socializer Bean");

			Context context = new InitialContext();
			socializer = (ISocializer) context
					.lookup("PhaseBook/ISocializer/local");

			String action = (String) message.getBody().get("action");

			if (action.equals("MAKE")) {
				log.info("Socializer MAKE");
				int origUserId = (Integer) message.getBody().get("origUserId");
				int destUserId = (Integer) message.getBody().get("destUserId");

				socializer.sendFriendshipInvitation(origUserId, destUserId);

			}
			if (action.equals("CONFIRM")) {
				log.info("Socializer CONFIRM");

				int uid = (Integer) message.getBody().get("uid");
				int requesterId = (Integer) message.getBody()
						.get("requesterId");
				socializer.confirmFriendshipRequest(uid, requesterId);

			}

			log.info("Socializer Bean called");
		} catch (Exception e) {
			log.error(e);
		}

		log.info("<------- ESB Action done");
		return message;

	}
}
